package com.tsystems.train.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.tsystems.train.dao.UserDAO;
import com.tsystems.train.dao.TicketDAO;
import com.tsystems.train.entity.User;
import com.tsystems.train.entity.Ticket;
import com.tsystems.train.entity.TimeTableElement;

/**
 * 
 * @author Turybrin Vladislav
 * 
 *         The class contains methods for performing basic operations with users
 */
@Stateless
public class UserServices {

	@EJB
	private UserDAO userDAO;

	@EJB
	private TicketDAO ticketDAO;

	/**
	 * Find user by login and match passwords. If it found and match is correct,
	 * login executed successfully.
	 * 
	 * @param login
	 * 
	 * @param password
	 * 
	 * @return "Login executed successfully" - if it so, otherwise message with
	 *         information about error.
	 */
	public String login(String login, String password) {
		User passenger = userDAO.getPassenger(login);
		if (passenger != null && passenger.getPassword().equals(password)) {
			return "Login executed successfully";
		}
		return "Login or password not correct!";

	}

	/**
	 * Get user by login
	 * 
	 * @param login
	 * @return
	 */

	public User getUser(String login) {
		return userDAO.getPassenger(login);

	}

	/**
	 * Register new user, if it has nоt registered yet.
	 * 
	 * @param passenger
	 *            - user, who want to register
	 * 
	 * @return "New user registered" - if reqistration was completed
	 *         successfully , otherwise "This user already exists."
	 */
	public String register(User passenger) {
		User passengerFound = userDAO.getPassenger(passenger.getLogin());

		if (passengerFound != null) {
			return "This user already exists.";
		}

		userDAO.insert(passenger);
		return "New user registered!";
	}

	/**
	 * Delete user, if it is registered
	 * 
	 * @param passenger
	 *            -user, who have to be deleted.
	 * 
	 * @return "User successfully deleted" - if it so, otherwise message with
	 *         information about error
	 */
	public String removePassenger(User passenger) {

		User passengerFound = userDAO.getPassenger(passenger.getLogin());

		if (passengerFound == null) {
			return "This user doesn't exist";
		}

		userDAO.remove(passengerFound);
		return "User successfully deleted";

	}

	/**
	 * Edit user, if it is registered
	 * 
	 * @param passenger
	 *            -user, who have to be editedd.
	 * 
	 * @return "User successfully changed" - if it so, otherwise message with
	 *         information about error
	 */
	public String editPassenger(User passenger) {

		User passengerFound = userDAO.getPassenger(passenger.getLogin());

		if (passengerFound == null) {
			return "This user doesn't exist";
		}

		passengerFound.setName(passenger.getName());
		passengerFound.setSurname(passenger.getSurname());
		passengerFound.setPassword(passenger.getPassword());
		passengerFound.setBirthday(passenger.getBirthday());
		passengerFound.setRole(passenger.getRole());

		userDAO.update(passengerFound);
		return "User successfully changed";

	}

	/**
	 * Get all users from database.
	 * 
	 * @return list of user.
	 */
	public List<User> viewPassengersBase() {

		List<User> passengerList = userDAO.getPassengerList();

		return passengerList;

	}

	/**
	 * Return list of users, who buy ticket on element of timetable
	 * 
	 * @param timeTableElement
	 * 
	 * @return list of users.
	 */
	public List<User> viewPassengersOnTrain(TimeTableElement timeTableElement) {

		List<Ticket> ticketList = ticketDAO.getTicketsByTrain(timeTableElement);
		HashSet<User> passengers = new HashSet<User>();
		for (Ticket ticket : ticketList) {
			passengers.add(ticket.getUser());
		}

		return new ArrayList<User>(passengers);

	}

}
